package javase.javase01.exercise03;
import java.io.*;


/**
 * @author: Gan
 * @date 2021/4/17 0017 14:37
 * @description: 2递归查找指定目录中(包括子目录中)，所有的.java文件，
 * 并且，把所有这些找到的java文件，复制(是复制不是移动)到一个指定的目录下
 * 目录结构同长风老师最后一次作业
 **/
public class work3 {
    public static void main(String[] args) throws IOException {
        File f = new File("D:\\firstLevel");
        findFile(f);
    }

    private static void findFile(File f) throws IOException {
        File[] files = f.listFiles(new MyJavaFilter());
        for (File file : files) {
            if (file.isDirectory()) {
                findFile(file);
            } else {
                copy(file);
            }
        }
    }

    private static void copy(File file) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\firstLevel1\\"+file.getName()));
        int readCount;
        char[] chars = new char[1024];
        while ((readCount = bufferedReader.read(chars)) != -1) {
            bufferedWriter.write(chars, 0, readCount);
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
}

class MyJavaFilter implements FileFilter {

    @Override
    public boolean accept(File pathname) {
        if (pathname.isDirectory()) {
            return true;
        }
        return pathname.getName().endsWith(".java");
    }
}